package com.itbaizhan.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

public class Demo3 {
    /*根据id获取元素：Element getElementById(String id)
    * 根据标签名获取素：Element getElementByTag(String tagName)
    * 根据属性获取元素：Element getElementByAttribute(String key)
    * 根据属性名=属性值获取元素：Element getElementByAttributeValue(String key,String value)
    * 根据选择器选取元素：Element select(String cssQuery)*/
    public static void main(String[] args) throws IOException {
        //获取文件路径
        String path = Demo3.class.getClassLoader().getResource("com/itbaizhan/xsd/student.xml").getPath();
        //获取对象
        Document document = Jsoup.parse(new File(path),"utf-8");

        //根据id获取元素
        Element bai_001 = document.getElementById("baizhan_0001");
        System.out.println(bai_001);

        System.out.println("----根据标签名获取素----");
        //获到的是元素集合
        Elements age = document.getElementsByTag("age");
        for(Element a:age){
            System.out.println(a);
        }

        System.out.println("------根据属性获取元素-----");
        Elements eng = document.getElementsByAttribute("english");
        for(Element element:eng){
            System.out.println(element);
        }

        System.out.println("------根据属性名=属性值获取元素-------");
        Elements elements = document.getElementsByAttributeValue("english","sx");
        for(Element ele:elements){
            System.out.println(ele);
        }

        System.out.println("根据css选择器选取元素");
        Elements select = document.select("#baizhan_0001");//根据id
        System.out.println(select);
        System.out.println("根据标签名");
        Elements sex = document.select("sex");
        System.out.println(sex);
        System.out.println("根据类属性");
        Elements a = document.select(".aa");
        System.out.println(a);
    }
}
